package com.shiwaixiangcun.customer.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.shiwaixiangcun.customer.Common;
import com.shiwaixiangcun.customer.ConfigSps;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.entity.LoginResultBean;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.network.HttpCallBack;
import com.shiwaixiangcun.customer.network.HttpRequest;
import com.shiwaixiangcun.customer.ui.activity.LoginActivity;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * @author Administrator
 * @date 2017/5/9
 */

public class RefreshTokenUtil implements Serializable {

    public static String BUG_TAG = "RefreshTokenUtil";

    /**
     * 刷新用户当前Token
     *
     * @param context
     * @param refreshToken
     */
    public static void refreshToken(final Context context, final String refreshToken) {
        HashMap<String, Object> hashMap = new HashMap<>(4);
        hashMap.put("client_id", GlobalConfig.clientId);
        hashMap.put("client_secret", GlobalConfig.clientSecret);
        hashMap.put("grant_type", "refresh_token");
        hashMap.put("refresh_token", refreshToken);

        HttpRequest.post(GlobalApi.INSTANCE.getLogin(), hashMap, new HttpCallBack() {

            @Override
            public void onSuccess(String responseJson) {
                Log.e(BUG_TAG, responseJson);
                Type type = new TypeToken<ResponseEntity<LoginResultBean>>() {
                }.getType();
                ResponseEntity<LoginResultBean> responseEntity = JsonUtil.fromJson(responseJson, type);

                if (responseEntity == null) {
                    Toast.makeText(context, "获取内容出错", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (responseEntity.getResponseCode() == 1003) {
                    String accessToken = responseEntity.getData().getAccessToken();
                    Log.e(BUG_TAG, accessToken);
                    AppSharePreferenceMgr.put(context, ConfigSps.CURRENT_TOKEN, responseEntity.getData().getAccessToken());
                    AppSharePreferenceMgr.put(context, ConfigSps.CURRENT_REFRESH_TOKEN, responseEntity.getData().getRefreshToken());
                    SharePreference.saveStringToSpParams(context, Common.ISORNOLOGIN, Common.SIORNOLOGIN, "yesLogin");
                    SharePreference.saveStringToSpParams(context, Common.IS_SAVE_LOGIN, Common.SISAVELOGIN, responseJson);


//                        sendInformationHttp(context);
                } else {
                    Toast.makeText(context, "token失效,重新登录", Toast.LENGTH_SHORT).show();
                    AppSharePreferenceMgr.put(context, ConfigSps.IS_LOGIN, Toast.LENGTH_SHORT);
                    SharePreference.saveStringToSpParams(context, Common.ISORNOLOGIN, Common.SIORNOLOGIN, "");
                    Intent intent = new Intent(context, LoginActivity.class);
                    context.startActivity(intent);


                }

            }

            @Override
            public void onFailed(Exception e) {
                Log.e(BUG_TAG, e.toString());
            }
        });


    }
}
